import java.io.IOException;

import javax.xml.stream.XMLStreamException;

public class ServerManager
{
	
	private networkHandler clientNwh; 
	private Map map;
	private XMLHandler clientXmlInput; 
	private RobotInterface robotInterface;
	private String robotName; 
	
	public ServerManager()
	{
		this.robotName = "Helge";
		this.clientNwh = new networkHandler(80);
		this.robotInterface = new RobotInterface(this.robotName);
		this.map = new Map();
		this.clientXmlInput = new XMLHandler();
		
	}
	
	//Iterate through client input, respond to commands like 
	//returning map and/or giving commands to robot. 
	public void handleClientInput() throws XMLStreamException
	{
		if(this.clientNwh.hasNext())
		{
			robotInterface.clear();
			clientXmlInput.fromXML(clientNwh.recieve());
			for(int i = 0; i < clientXmlInput.getCommand().size(); i++) //xml or not?
			{
				
				if(this.clientXmlInput.getCommand().elementAt(i).isMove()) //Move
				{
					this.robotInterface.addMoveDistance(this.clientXmlInput.getCommand().elementAt(i).getValueOfParameter("req"));
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isReboot()) //Reboot
				{
					this.robotInterface.addReboot();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isRotate()) //Rotate
				{
					this.robotInterface.addRotate(this.clientXmlInput.getCommand().elementAt(i).getValueOfParameter("req"));
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isScan()) //Scan
				{
					this.robotInterface.addScan( this.clientXmlInput.getCommand().elementAt(i).getValueOfParameter("req"));
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isClose()) //Close
				{
					this.robotInterface.addClose();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isMoveForward()) //MoveForward
				{
					this.robotInterface.addMoveForward();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isMoveBackward()) //MoveBackward
				{
					this.robotInterface.addMoveBackward();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isStop()) //Stop
				{
					this.robotInterface.addStop();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isAutoMode()) //AutoMode
				{
					this.robotInterface.addAutoMode();
				}
				else if(this.clientXmlInput.getCommand().elementAt(i).isMapRequest()) //add this to ServerCommands
				{
					clientNwh.sendImg(map.Yeild_Map());
				}
			}
		}
	}
	
	//iterate through robot input, update map 
	//could possibly be passed directly to MapManager, if it can handle xmlfiles, and if 
	//we can expect the xmlfile to contain nothing but map information
	public void handleRobotInput() throws XMLStreamException, IOException
	{
		robotInterface.clear();
		robotInterface.recieve();
		map.Update(robotInterface.getInXMLHandler());
		//Also send webcam image to client
	}
	
	//manager for distributing attention between 
	//different networksockets and the BTC we need to listen for. 
	public void attentionManager() throws InterruptedException, IOException, XMLStreamException
	{
		while(true)
		{
			if(clientNwh.isConnected())
			{
				clientNwh.send("<s2c></s2c>"); //dont know what we'll send here. 
				Thread.sleep(1000);
				handleRobotInput();
			}
			if(robotInterface.isConnected())
			{
				robotInterface.send("<s2r></s2r>");
				Thread.sleep(1000);
				handleClientInput();
			}
		}
	}
	
	public static void main(String [] args)
	{
		ServerManager sm = new ServerManager();
		//Waiting for client connection. Required constant attention?, 
		//or can we just let the robot be in automode communicating with the server, while server passively waiting for client to connect? 
	}
}